/// <reference path="../../sage/sage.util.js" />

var FourTriangleTreePainter = Base.extend({
    initialize: function initialize() {
        this.TREE_TRIANGLES = 4;
        this.OPTIMAL_DISTANCE = 1500;
    },
    paint: function paint(context) {
        var location = context.gameObject.locationRelativeToCamera;
        if (location.visible) {
            var distance = location.distance;

            var rect = location.objectRectangle,
                x = rect.x,
                y = rect.y,
                width = rect.w,
                height = rect.h;

            if (distance < this.OPTIMAL_DISTANCE) {
                for (var i = 0; i < this.TREE_TRIANGLES; i++) {
                    var j = this.TREE_TRIANGLES - i - 1;
                    var l = x,
                        r = (x + width),
                        w = Math.abs(r - l),
                        w_off = w * (j * 0.10);
                    l += w_off;
                    r -= w_off;

                    context.frontLayer.fillStyle = context.gameObject.color;
                    context.frontLayer.beginPath();
                    context.frontLayer.moveTo(x + width / 2, y + (i * height / this.TREE_TRIANGLES) - height / 4);
                    context.frontLayer.lineTo(l, y + (i * height / this.TREE_TRIANGLES) + height / this.TREE_TRIANGLES);
                    context.frontLayer.lineTo(r, y + (i * height / this.TREE_TRIANGLES) + height / this.TREE_TRIANGLES);
                    context.frontLayer.closePath();
                    context.frontLayer.fill();

                    //context.frontLayer.fillStyle = context.gameObject.textColor;
                    //context.frontLayer.fillText("" + util.degrees(location.placement.angleFromCamera), x, y - 5);
                }
            } else {
                context.frontLayer.fillStyle = context.gameObject.color;
                context.frontLayer.beginPath();
                context.frontLayer.moveTo(x + width / 2, y);
                context.frontLayer.lineTo(x, y + height);
                context.frontLayer.lineTo(x + width, y + height);
                context.frontLayer.closePath();
                context.frontLayer.fill();
            }
        }
    }
});
